﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace HardMobCommons
{
	public static class StringExtensions
	{
		public static int ProcuraNumeros(this string link)
		{
			string numeros = string.Empty;

			foreach (char c in link)
			{
				int numero;
				if (int.TryParse(c.ToString(), out numero))
					numeros += numero.ToString();
				else
					break;
			}

			if (!string.IsNullOrEmpty(numeros))
				return int.Parse(numeros);

			throw new Exception("Número não encontrado");
		}

		public static T TryParser<T>(this string input)
		{
			T retorno = default(T);
			try
			{
				Type typeToConvert = typeof(T);
				TypeConverter converter = TypeDescriptor.GetConverter(typeToConvert);
				retorno = (T)converter.ConvertFromString(input);
			}
			catch (Exception)
			{
			}

			return retorno;
		}
	}
}
